<template>
  <div class="login">
    <div class="login-box">
      <h2 class="title">忘记密码</h2>
      <el-form :model="form" :rules="rules" ref="form" class="ms-content">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" maxlength="11" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item prop="verify">
          <el-input class="handle-search" placeholder="输入验证码" v-model="form.verify"></el-input>
          <el-button style="width: 112px;" @click="sendSms">{{codeMsg}}</el-button>
        </el-form-item>
        <!-- <el-form-item prop="imgCode">
          <div style="display: flex;">
            <el-input class="handle-search" placeholder="输入图形验证码" v-model="form.imgCode">
            </el-input>
            <el-image class="code" ></el-image>
      
          </div>
        </el-form-item> -->
        <div class="password-setting">
          <div @click="$router.push('/login')">去登录</div>
        </div>
        <el-form-item class="login-btn">
          <el-button type="primary" @click="submitForm">下一步</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
  import apis from '@/apis/modules/login'
  export default {
    data() {
      //手机号自定义规则
      var validatorMobile = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入手机号"));
        } else {
          var isPhone = /^[1][0-9][0-9]{9}$/;
          if (!isPhone.test(value)) {
            callback(new Error("手机号输入错误"));
          } else {
            this.canClick = true;
            callback();
          }
        }
      };
      //手机验证码规则
      var validatorVerify = (rule, value, callback) => {
        if (value.trim() === "") {
          callback(new Error("请输入手机验证码"));
        } else {
          callback();
        }
      };
      //图片验证码规则
      var validatorImgCode = (rule, value, callback) => {
        if (value.trim() === "") {
          callback(new Error("请输入图形验证码"));
        } else {

          if (this.form.imgCode != this.identifyCode) {
            callback(new Error("图形验证码错误"));
          } else {

            callback();
          }
        }
      };
      return {
        form: {
          mobile: '',
          verify: '',
          // imgCode:''
        },
        rules: {
          mobile: [{ validator: validatorMobile, trigger: "blur" }],
          verify: [{ validator: validatorVerify, trigger: "blur" }],
          // imgCode: [{ validator: validatorImgCode, trigger: "blur" }],
        },

        codeMsg: "获取验证码",
        canClick: false,
        totalTime: 60,
        clock: null,
      }
    },

    methods: {
      // 获取手机验证码
      async sendSms() {
        if (!this.canClick) return;
        this.canClick = false;
        this.codeMsg = this.totalTime + "s";
        //60秒倒计时
        this.clock = window.setInterval(() => {
          this.totalTime--;
          this.codeMsg = this.totalTime + "s";
          if (this.totalTime < 0) {
            window.clearInterval(this.clock);
            this.codeMsg = "重新发送";
            this.totalTime = 60;
            this.canClick = true; //这里重新开启
          }
        }, 1000);
        const { code, data, msg } = await apis.sendSms({
          mobile: this.form.mobile
        })
        if (code == 200) {
          this.$message.success(msg)
        }
      },
      // 下一步
      submitForm() {
        this.$refs.form.validate(async valid => {
          if (valid) {
            const { code, data, msg } = await apis.forgetVerify(this.form);
            console.log(code, data, msg)
            if (code == 200) {
              this.$message.success(msg)
              this.$router.push('/Next')
            } else {
              this.$message.error(msg)
            }
          }
        })
      }
    },
  }
</script>
<style lang="scss" scoped>
  .handle-search {
    width: 240px;
    height: 40px;
    margin-right: 15px;
  }

  .login {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../assets/img/bg.jpg);
  }

  .login-box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 574px;
    height: 428px;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    align-items: center;


    .title {
      margin: 30px 0;


    }

    .ms-content {
      width: 368px;

      .password-setting {
        display: flex;
        justify-content: flex-end;

        color: #1890FF;
        font-size: 13px;
        cursor: pointer;

      }

      .login-btn button {
        margin-top: 30px;
        width: 100%;
      }

      .login-tips {
        text-align: center;
        color: #333;
        font-size: 13px;
      }
    }
  }
</style>